package org.leiyang.common.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.leiyang.common.util.SystemClock;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 一对一的单聊消息
 * 后面需要考虑分表存储
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table("msg_one_to_one")
public class MsgOneToOne implements Serializable, Comparable<MsgOneToOne>{
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    /**
     * 发送者ID
     * 注: 驼峰写法的字段，必须要指定与表字段的对应关系
     */
    @Column("fromUserId")
    private Long fromUserId;

    /**
     * 接受者ID
     */
    @Column("toUserId")
    private Long toUserId;

    /**
     * 消息类型，2表示文本消息，3表示音频消息，4表示视频消息，5表示图片消息
     * 与{@link org.leiyang.common.dtos.MessageType}保持一致
     */
    @Column("msgType")
    private Integer msgType;

    /**
     * 文本消息内容
     */
    @Column("textMsg")
    private String textMsg;

    /**
     * 音视频文件的远程URL
     */
    @Column("avMsgUrl")
    private String avMsgUrl;

    /**
     * 图片文件宽度，单位: 像素
     */
    @Column("imgWidth")
    private Integer imgWidth;

    /**
     * 图片文件高度，单位: 像素
     */
    @Column("imgHeight")
    private Integer imgHeight;

    /**
     * 是否已发送给目标用户，0表示未接收，1表示已接收
     */
    @Column("send")
    private Integer send;

    /**
     * 消息发送的时间戳
     */
    @Column("time")
    private Long time;

    /**
     * 将Map格式数据转化为MsgOneToOne
     * @param msgMap Map格式数据
     * @return MsgOneToOne
     */
    public static MsgOneToOne convertFromMap(Map<String, String> msgMap) {
        final Long msgId = msgMap.containsKey("id") ? Long.parseLong(msgMap.get("id")) : null;
        return MsgOneToOne.builder().fromUserId(Long.parseLong(msgMap.getOrDefault("fromUserId", "-1")))
                .toUserId(Long.parseLong(msgMap.getOrDefault("toUserId", "-1")))
                .msgType(Integer.parseInt(msgMap.getOrDefault("msgType", "0")))
                .textMsg(msgMap.getOrDefault("textMsg", ""))
                .avMsgUrl(msgMap.getOrDefault("avMsgUrl", ""))
                .imgWidth(Integer.parseInt(msgMap.getOrDefault("imgWidth", "0")))
                .imgHeight(Integer.parseInt(msgMap.getOrDefault("imgHeight", "0")))
                .send(Integer.parseInt(msgMap.getOrDefault("send", "0")))
                .time(Long.parseLong(msgMap.getOrDefault("time", String.valueOf(SystemClock.now()))))
                .id(msgId)
                .build();
    }

    /**
     * 将MsgOneToOne转化为Map
     * @param msgOneToOne MsgOneToOne
     * @return Map
     */
    public static Map<String, String> convertToMap(MsgOneToOne msgOneToOne) {
        Map<String, String> msgMap = new HashMap<>(6);
        msgMap.put("fromUserId", msgOneToOne.getFromUserId().toString());
        msgMap.put("toUserId", msgOneToOne.getToUserId().toString());
        msgMap.put("msgType", msgOneToOne.getMsgType().toString());
        msgMap.put("textMsg", msgOneToOne.getTextMsg());
        msgMap.put("avMsgUrl", msgOneToOne.getAvMsgUrl());
        msgMap.put("imgWidth", Objects.isNull(msgOneToOne.getImgWidth()) ? "0" : msgOneToOne.getImgWidth().toString());
        msgMap.put("imgHeight", Objects.isNull(msgOneToOne.getImgHeight()) ? "0" : msgOneToOne.getImgHeight().toString());
        msgMap.put("send", msgOneToOne.getSend().toString());
        msgMap.put("time", String.valueOf(SystemClock.now()));
        return msgMap;
    }

    @Override
    public int compareTo(MsgOneToOne o) {
        return (int) (this.id - o.getId());
    }
}
